package zkk.com.mengqu.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.User;

/**
 * Created by Administrator on 2017/6/8.
 */
public class RegisterActivity extends Activity implements View.OnClickListener{
    private Context context;
    private ImageView back;//返回
    private EditText etUserPhone, etUserPwd;//用户账号，用户密码
    private Button register;//注册

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    public void initView(){
        context = getApplicationContext();
        back = (ImageView)findViewById(R.id.iv_back);
        etUserPhone = (EditText)findViewById(R.id.et_user_phone);
        etUserPwd = (EditText)findViewById(R.id.et_user_pwd);
        register = (Button)findViewById(R.id.btn_register);

        back.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_register:
                String phone = etUserPhone.getText().toString().trim();
                String pwd = etUserPwd.getText().toString().trim();
                register(phone, pwd);
                break;
            default:
                break;
        }
    }

    public void register(String phone, String pwd){
        APIManager.setManager()
                .register(phone, pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onCompleted() {
                        Log.w("Register","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w("Register","onError");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(User user) {
                        Log.w("Register","注册成功--" + user);
                        Toast.makeText(context , "注册成功" , Toast.LENGTH_LONG).show();
                        finish();
                    }
                });

    }
}
